<template>
  <div>
    <van-icon
      @click="loading"
      :color="value === 1 ? 'red' : '#777'"
      name="good-job-o"
    />
  </div>
</template>

<script>
import { goodArticle, removegoodArticle } from '@/api/article'

export default {
  name: 'GoodJob',
  props: {
    value: {
      type: Number,
      required: true
    },
    aut_id: {
      type: String,
      required: true
    }
  },
  methods: {
    // 点赞/取消点赞
    async loading() {
      try {
        let status = ''
        if (this.value !== 1) {
          // 点赞
          await goodArticle({
            target: this.aut_id
          })
          status = 1
        } else {
          // 取消点赞
          await removegoodArticle(this.aut_id)
          status = -1
        }
        this.$emit('input', status)
        this.$toast(this.value !== 1 ? '点赞成功' : '取消点赞')
      } catch (error) {
        this.$toast('操作失败')
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
